<?php
//mysql_connect("localhost", "root", "") or die(mysql_error());
//mysql_select_db("cs499") or die(mysql_error());

//Database class
class Database {
  protected $db_host;
  protected $db_user;
  protected $db_password;
  protected $db_name;

  // Establish database connection
  public function __construct() {
    $this->db_host = 'localhost';
    $this->db_name = 'cs499';
    $this->db_user = 'root';
    $this->db_password = '';
    $db_conn = mysql_connect($this->db_host, $this->db_user, $this->db_password);
    $db_select = mysql_select_db($this->db_name, $db_conn);
  }

  /*
   * Get all the vehicle's info
   */
  public function get_all($year,$make,$model,$transmission) {
    $result = mysql_query("
      SELECT *
      FROM vehicleData
      WHERE modelYear = '$year'
      AND mfrName = '$make'
      AND carline = '$model'
      AND transmission = '$transmission'
    ");
  $record = mysql_fetch_array($result);
  return $record;
  }
  /*
   * Get vehicle year
   */
  public function get_year() {
    $result = mysql_query("
      SELECT DISTINCT modelYear
      FROM vehicleData
      ORDER BY modelYear DESC")
      or die(mysql_error()
    );
    return $result;
  }

  /*
   * Get vehicle makers
   */
  public function get_maker($year) {
    $result = mysql_query("
      SELECT DISTINCT mfrName
      FROM vehicleData
      WHERE modelYear = '$year'
      ORDER BY mfrName")
      or die(mysql_error());
    return $result;
  }

  /*
   * Get vehicle models
   */
  public function get_models($year, $maker) {
    $result = mysql_query("
      SELECT DISTINCT carline
      FROM vehicleData
      WHERE mfrName = '$maker'
      AND modelYear = '$year'
      ORDER BY carline")
      or die(mysql_error());
    return $result;
  }

  /*
   * Get vehicle transmission
   */
  public function get_vehicle_transmission($year, $maker,$model) {
    $result = mysql_query("
      SELECT DISTINCT transmission
      FROM vehicleData
      WHERE modelYear = '$year'
      AND mfrName = '$maker'
      AND carline = '$model'
      ORDER BY transmission")
      or die(mysql_error());
    return $result;
  }

}

?>